# quadrat based phylogenetic community pattern analysis
# test is both side t test
# Author: Guochun Shen
# Data:   2012-1-2
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

phyloquad_null=function(com,phyd,maxsize,step=1,nsim=199,abadjust=FALSE,usepicante=FALSE){
	qsize=seq(step,maxsize,step)
	xmax=com$win$xrange[2]
	nxys=round(xmax/qsize)
	nxys=unique(nxys)
	pvalues=numeric()
	for(i in 1:length(nxys)){
		if(!usepicante){
			pvalues[i]=quadrat_phylo(com,phyd,nxy=nxys[i],nsim=nsim,abadjust=abadjust)[1]
		}else{
			qua_com=quadratize(com,nxys[i],nxys[i])
			comtable=table(qua_com$marks$ploti,qua_com$marks$sp)
			tempre=ses.mpd(comtable,phyd,null.model="taxa.labels",abundance.weighted=abadjust)
			ses=(tempre$mpd.rand.mean-tempre$mpd.obs)/tempre$mpd.rand.sd
			pvalues[i]=t.test(ses)$p.value
		}
		
	}
	pvalues=round(pvalues,4)
	r=xmax/nxys
	return(data.frame(r=r,pvalues=pvalues))
}

